<?php
//Array Class
class ArrayClass{
	public function __construct(){
		srand((double) microtime()*10000000);
	}
	
	function array_max($array){	//get the max. value of an array
		$max = $array[0];
		$index = 0;
		foreach($array as $key=>$val)
			if($val > $max){
				$max = $val;
				$index = $key;
			}
		return array("index"=>$index, "value"=>$max);
	}
	
	function array_min($array){	//get the min. value of an array
		$min = $array[0];
		$index = 0;
		foreach($array as $key=>$val)
			if($val < $min){
				$min = $val;
				$index = $key;
			}
		return array("index"=>$index, "value"=>$min);
	}
	
	function array_sum($array){	//get the sum. of an array
		$sum = 0;
		foreach($array as $val)
			$sum += $val;
		return $sum;
	}
	
	function array_avg($array){	//get the avg. value of an array
		return $this->array_sum($array)/count($array);
	}
	
	function array_mode($array){	//get the mode value of an array
		foreach($array as $val)
			$frequency[$val]++;
		$r = $this->array_max($frequency);
		return $r["index"];
	}
	
	function array_firstQuartile($array){	//get the first quartile of an array
		$count = count($array);
		$array = $this->array_order($array);
		$n = $count*0.25;
		if(ceil($n) == $n) return ($array[$n-1]+$array[$n])/2;
		else return $array[ceil($n)-1];
	}
	
	function array_median($array){	//get the median/second quartile value of an array
		$count = count($array);
		$array = $this->array_order($array);
		$n = $count/2;
		if(ceil($n) == $n) return ($array[$n-1]+$array[$n])/2;
		else return $array[ceil($n)-1];
	}
	
	function array_thirdQuartile($array){	//get the third quartile value of an array
		$count = count($array);
		$array = $this->array_order($array);
		$n = $count*0.75;
		if(ceil($n) == $n) return ($array[$n-1]+$array[$n])/2;
		else return $array[ceil($n)-1];
	}
	
	function array_order($array, $ascending=true){	//get array in order
		if($ascending) usort($array, cmp_a);
		else usort($array, cmp_d);
		return $array;
	}
	
	function array2str($value){	//change a array to a string
		if(is_array($value)) return base64_encode(serialize($value));
		else return false;
	}
	
	function str2array($value){	//recover a array to a string
		if(is_string($value)) return unserialize(base64_decode($value));
		else return false;
	}
}
	
function cmp_a($a, $b) {
  if ($a == $b) return 0;
  return ($a < $b) ? -1 : 1;
}

function cmp_d($a,$b) {
  if ($a == $b) return 0;
  return ($a > $b) ? -1 : 1;
}
?>